package sr.myserver.controller;

import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sr.myserver.commons.Constants;
import sr.myserver.commons.Result;
import sr.myserver.commons.TokenManager;
import sr.myserver.config.JwtConfig;
import sr.myserver.service.LoginService;
import sr.myserver.vo.Login;
import sr.myserver.vo.Passwd;
import sr.myserver.vo.common.Basic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 安全认证
 *
 * @author sirui
 */
@Slf4j
@RestController
@RequestMapping("/authorized")
@Api(tags = "安全认证模块")
public class SysAuthorizedController {

    @Autowired
    LoginService loginService;

    /**
     * 系统登录
     *
     * @param entity   登录参数
     * @param response
     * @return
     */
    @PostMapping
    @Operation(summary = "系统登录")
    public Basic.Response authorized(@RequestBody Login.Request entity, HttpServletResponse response) {
        if (!StringUtils.hasText(entity.getUserId()) || !StringUtils.hasText(entity.getPasswd())) {
            return Result.fail("输入参数不正确");
        }
        return loginService.login(entity, response);
    }

    /**
     * token刷新
     *
     * @param entity
     * @return
     */
    @PostMapping("/refresh")
    @Operation(summary = "token刷新")
    public Basic.Response refresh(@RequestBody Basic.Request entity) {
        if (!StringUtils.hasText(entity.getToken())) {
            return Result.fail("获取认证信息失败");
        }

        Claims claims = JwtConfig.thinning(entity.getToken());
        if (claims == null) {
            return Result.fail("读取token信息失败");
        }
        String jwtToken = JwtConfig.refresh(claims);
        loginService.add(JwtConfig.getUserId(claims), jwtToken);
        return Result.ok(jwtToken);
    }

    /**
     * 修改密码
     *
     * @param entity
     * @param req
     * @param res
     * @param token
     * @return
     */
    @PutMapping
    @Operation(summary = "修改密码")
    public Basic.Response update(@RequestBody Passwd.Request entity, HttpServletRequest req, HttpServletResponse res,
                                 @CookieValue(Constants.AUTHORIZATION) String token) {
        if (!StringUtils.hasText(entity.getPasswd()) || !StringUtils.hasText(entity.getNewPasswd())) {
            return Result.fail("输入参数不正确");
        }

        Basic.Response result = loginService.updatePass(entity);
        if (Result.OK.equals(result.getCode())) {
            TokenManager.delCookie(req, res, Constants.AUTHORIZATION);
            String userId = entity.getUserId();
            loginService.remove(userId);
        }
        return result;
    }

    /**
     * 退出登录
     *
     * @param req
     * @param res
     * @param token
     * @return
     */
    @DeleteMapping
    @Operation(summary = "退出登录")
    public Basic.Response logout(HttpServletRequest req, HttpServletResponse res, @CookieValue(Constants.AUTHORIZATION) String token) {
        String userId = JwtConfig.getUserId(token);
        loginService.remove(userId);
        TokenManager.delCookie(req, res, Constants.AUTHORIZATION);
        return Result.ok();
    }

}